package com.sgb.jicai.viewmodel;

import androidx.lifecycle.ViewModel;

import com.sgb.jicai.base.JiCaiBaseApi;
import com.sgb.jicai.entity.JcMySupervisionEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.jicai.entity.JcMyFactoryDataEntity;
import com.sgb.jicai.view.activity.MyJicaiSupervisionActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyJicaiSupervisionModel extends ViewModel {

    private MyJicaiSupervisionActivity activity;
    public boolean hasNext = false;//是否有下一页

    public void getTypeList() {
        JiCaiBaseApi.getApi().getAccusationItem().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data.getData() == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    List<PickSelectEntity> datas = data.getData();
                    //手动添加上全部
                    PickSelectEntity entity = new PickSelectEntity();
                    entity.setNo("");
                    entity.setProjectscale("全部");
                    datas.add(0, entity);
                    activity.setTypeDatas(datas);

                }
            }
        });

    }

    //集采状态
    public void getStatusList() {
        List<PickSelectEntity> statusDatas = new ArrayList<>();
        PickSelectEntity entity0 = new PickSelectEntity();
        entity0.setNo("");
        entity0.setProjectscale("全部");
        PickSelectEntity entity1 = new PickSelectEntity();
        entity1.setNo("1");
        entity1.setProjectscale("已处理");
        PickSelectEntity entity2 = new PickSelectEntity();
        entity2.setNo("0");
        entity2.setProjectscale("未处理");
        statusDatas.add(entity0);
        statusDatas.add(entity1);
        statusDatas.add(entity2);
        activity.setStatusDatas(statusDatas);
    }

    //获取我的监督列表
    public void getList(String accusationItem, String status, int pageNum) {
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        if (null != accusationItem && !accusationItem.isEmpty()) {
            map.put("accusationItem", accusationItem);
        }
        if (null != status && !status.isEmpty()) {
            map.put("status", status);
        }
        map.put("pageNum", String.valueOf(pageNum));
        map.put("pageSize", "10");
        JiCaiBaseApi.getApi().getMySuperviseListData(map).enqueue(new ICallBack<BaseEntity<JcMyFactoryDataEntity<JcMySupervisionEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<JcMyFactoryDataEntity<JcMySupervisionEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null)
                    return;
                if (null == data.getData())
                    return;
                if (data.getData().getCurrent() < data.getData().getPages()) {
                    hasNext = true;
                } else {
                    hasNext = false;
                }
                if (null == data.getData().getRecords()) {
                    return;
                }
                activity.showList(data.getData().getRecords(), hasNext);
            }
        });
    }

    //我的监督跳转到h5页面
    public void getH5Url(JcMySupervisionEntity entity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        JiCaiBaseApi.getApi().getUrlSetting("mySupervision").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getData() == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(JiCaiBaseApi.RESPONSE_OK)) {
                    activity.showUrl(data.getData().getUrl() + "?id=" + entity.getId());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }


    public void setActivity(MyJicaiSupervisionActivity myJicaiSupervisionActivity) {
        this.activity=myJicaiSupervisionActivity;
    }
}
